package com.example.ccod.nacos.gateway.controller;

import com.ccod.nacos.goods.facade.GoodsDto;
import com.ccod.nacos.order.model.OrderDTO;
import com.example.ccod.nacos.gateway.client.GoodsFacadeClient;
import com.example.ccod.nacos.gateway.client.OrderFacadeClient;
import io.seata.common.util.StringUtils;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author: ccod
 * @data: 2021/1/1 12:48
 **/
@Service
public class SeataService {

    @Autowired
    private GoodsFacadeClient goodsFacadeClient;

    @Autowired
    private OrderFacadeClient orderFacadeClient;

    @GlobalTransactional(name = "test-seata",rollbackFor = Exception.class)
    public String testSeata(String enable){
        GoodsDto goodsDto = new GoodsDto();
        goodsDto.setGoodsId("1008611");
        goodsDto.setDesc("分布式事务商品");
        goodsDto.setPrice("18.32");
        goodsFacadeClient.addGoods(goodsDto);

        OrderDTO orderDTO = new OrderDTO();
        orderDTO.setOrderId("1008611");
        orderDTO.setCreateTime(new Date());
        orderDTO.setPrice("18.32");
        orderDTO.setUpdateTime(new Date());
        orderFacadeClient.createOrder(orderDTO);
        if(StringUtils.isNotEmpty(enable)){
            System.out.println(1/0);
        }
        return "SUCCESS";
    }

}
